package com.jiyongjun.bms.base.module.menu.service.impl;

import com.github.pagehelper.PageHelper;
import com.jiyongjun.bms.base.common.core.BaseMapper;
import com.jiyongjun.bms.base.common.core.BaseServiceImpl;
import com.jiyongjun.bms.base.module.menu.dao.MenuDao;
import com.jiyongjun.bms.base.module.menu.entity.MenuEntity;
import com.jiyongjun.bms.base.module.menu.entity.MenuQueryModel;
import com.jiyongjun.bms.base.module.menu.service.MenuService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h2>系统菜单表服务实现类</h2>
 * <p>
 *  描述: 
 * </p>
 *
 * @author 冀大侠
 * @since 1.0 2019-06-01 22:21:11
 */
@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<MenuEntity, MenuQueryModel, Long> implements MenuService {
    @Resource
    private MenuDao menuDao;
    
    @Override
    protected BaseMapper<MenuEntity> getMapper() {
        return menuDao;
    }

    @Override
    public List<MenuEntity> getByCondition(MenuQueryModel queryModel) {
        if (queryModel.getPageSize() != null && queryModel.getPageNo() != null) {
            PageHelper.startPage(queryModel.getPageNo(), queryModel.getPageSize());
        }
        Sqls sqls = Sqls.custom();
        if (queryModel.getMenuName() != null) {
            sqls.andLike("name", "%" + queryModel.getMenuName() + "%");
        }
        if (queryModel.getMenuType() != null) {
            sqls.andEqualTo("type", queryModel.getMenuType());
        }
        Example example = Example.builder(queryModel.getEntityClass()).andWhere(sqls).build();

        // 也可以用下面的写法
       /* Weekend<? extends MenuEntity> weekend = Weekend.of(queryModel.getClass());
        WeekendCriteria<? extends MenuEntity, Object> weekendCriteria = weekend.weekendCriteria();
        if (queryModel.getName() != null) {
            weekendCriteria.andLike(MenuEntity::getName, "%" + queryModel.getName() + "%");
        }
        if (queryModel.getType() != null) {
            weekendCriteria.andLike(MenuEntity::getType, "%" + queryModel.getType() + "%");
        }
        return this.menuDao.selectByExample(weekend);*/
        return this.menuDao.selectByExample(example);
    }
}